package com.lxd.order.task;


import com.lxd.order.mapper.OrderMapper;
import com.lxd.order.pojo.MqLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ConsumeTask {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Scheduled(cron = "0/10 * * * * ? ")
    public void consumeTask(){
        List<MqLog> mqLogs = orderMapper.selectMqLog("unfinished");
        if (mqLogs.isEmpty())
            return;
        for (MqLog mqLog:mqLogs){
            rabbitTemplate.convertAndSend("consume.fanout","",mqLog);
            orderMapper.updateMqLog(mqLog.getId());
        }
    }

}
